package com.xuexiang.xuidemo.provider;
import com.isotne.glidelibrary.utils.OhosGlide;
import com.xuexiang.xui_lib.util.DisplayUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.io.IOException;
import java.util.List;

public class XulVideoListProvider extends BaseItemProvider {
    private List<ImageViewInfo> list;
    private AbilitySlice slice;
    OnItemClickListener mOnItemClickListener;
    public XulVideoListProvider(List<ImageViewInfo> list, AbilitySlice slice,OnItemClickListener mOnItemClickListener) {
        this.list = list;
        this.slice = slice;
        this.mOnItemClickListener=mOnItemClickListener;
    }
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()){
            return list.get(position);
        }
        return null;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_xui_video_item, null, false);
        } else {
            cpt = convertComponent;
        }
        Image image = (Image) cpt.findComponentById(ResourceTable.Id_image);
        if (list != null) {
            ImageViewInfo user = list.get(position);
            String imgUrl = user.getUrl();
            if (imgUrl != null) {
                  OhosGlide.with(slice).load(user.getUrl()).def(ResourceTable.Media_xui_ic_default_img).into(image);
            }
        }
        image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mOnItemClickListener.onItemClick(component,position);
            }
        });
        int width = DisplayUtils.getDisplayWidth(slice);
        int cloumn = width / 2;
        cpt.setWidth(cloumn);
        return cpt;
    }
    public interface OnItemClickListener {
        void onItemClick(Component component, int position);
    }
}


